package com.mouse.haoi.service;


import com.mouse.haoi.utils.HttpClientUtils;
import com.mouse.haoi.utils.ImageChange64Bit;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ImageService {

    public HttpClientUtils httpClientUtils = new HttpClientUtils();
    private static String accessPath = "";


    private String getAccessPath() {
        if (accessPath != null && !"".equals(accessPath)) return accessPath;
        Map<String, Object> paramMap = new HashMap();
        String responseStr = httpClientUtils.get("http://2.haoi23.net/svlist.html", paramMap);
        String host = null;
        Pattern pattern = Pattern.compile("===.*\\+\\+\\+");
        Matcher matcher = pattern
                .matcher(responseStr);
        if (matcher.find())
            host = matcher.group(0);
//		host = host.replace("\\=\\=\\=", "");
//		host = host.replace("\\+\\+\\+", "");
        host = host.substring(3, host.length() - 3);
        String[] s = host.split("--");
        String actionUrl = "http://" + s[0];
        accessPath = actionUrl;
        return actionUrl;
    }

    public String sendFile(Map<String, Object> paramMap, String imagePath) {
        String host = getAccessPath();
        ImageChange64Bit bit = new ImageChange64Bit();
        String image = bit.GetImageStr(imagePath);//"d:\\test\\captcha.jpg"
        paramMap.put("DaiLi", "haoi");//【不公开】
        paramMap.put("Ver", "web2");//【不公开】
        paramMap.put("Img", image);
        String subjectId = httpClientUtils.post(host + "/UPLOADBASE64.ASPX", paramMap);
        return subjectId;
    }

    public String sendFile(Map<String, Object> paramMap, InputStream fileInputStream) {
        String host = getAccessPath();
        ImageChange64Bit bit = new ImageChange64Bit();
        String image = bit.GetImageStr(fileInputStream);//"d:\\test\\captcha.jpg"
        paramMap.put("DaiLi", "haoi");//【不公开】
        paramMap.put("Ver", "web2");//【不公开】
        paramMap.put("Img", image);
        String subjectId = httpClientUtils.post(host + "/UPLOADBASE64.ASPX", paramMap);
        return subjectId;
    }
    public String getAnswer(Map<String, Object> paramMap) {
        String host = getAccessPath();
        String responseStr = httpClientUtils.post(host + "/GetAnswer.aspx", paramMap);
        return responseStr;
    }

    public String sendError(Map<String, Object> paramMap) {
        String host = getAccessPath();
        String responseStr = httpClientUtils.post(host + "/SendError.aspx", paramMap);
        return responseStr;
    }

    public String getPoint(Map<String, Object> paramMap) {
        String host = getAccessPath();
        String responseStr = httpClientUtils.post(host + "/GetPoint.aspx", paramMap);
        return responseStr;
    }

}
